package com.aerozhonghuan.logisticsmall.modules.sort;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.URLs;
import com.aerozhonghuan.logisticsmall.modules.sort.beans.TreeBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenLin on 2019/5/22 15:08.Email&WeChat:7877409@163.com
 */
public class ProductCategoryExpandableAdapter extends BaseExpandableListAdapter {

    // 用来装载某个item是否被选中
    SparseBooleanArray selected;
    int old = -1;
    int parentPosition = -1;
    Animation mScaleBigAnim;
    private boolean isSelect;
    private Context mContext;
    private List<TreeBean.Data> mGroupArrList = new ArrayList<TreeBean.Data>();

    public ProductCategoryExpandableAdapter(Context c, Animation scaleBigAnim) {
        mContext = c;
        selected = new SparseBooleanArray();
        mScaleBigAnim = scaleBigAnim;
    }

    public void setSelectedGroupItem(int groupPosition) {
        this.parentPosition = groupPosition;
        this.selected.put(old, false);
    }

    public void setSelectedChildItem(int groupPosition, int childPosition) {
        this.parentPosition = groupPosition;
        if (old != -1) {
            this.selected.put(old, false);
        }
        this.selected.put(childPosition, true);
        old = childPosition;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public void setData(ArrayList<TreeBean.Data> groupList) {
        if (groupList == null) {
            mGroupArrList.clear();
        } else {
            mGroupArrList = groupList;
        }
    }

    @Override
    public int getGroupCount() {
        if (mGroupArrList == null || mGroupArrList.size() == 0) {
            return 0;
        }
        return mGroupArrList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (null == mGroupArrList || null == mGroupArrList.get(groupPosition).childs
                || mGroupArrList.get(groupPosition).childs.size() == 0) {
            return 0;
        }
        return mGroupArrList.get(groupPosition).childs.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupArrList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mGroupArrList.get(groupPosition).childs.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            groupHolder = new GroupHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.exablelist_group_item, null);
            groupHolder.selectLine = (View) convertView.findViewById(R.id.select_line);
            groupHolder.groupTitleView = (TextView) convertView.findViewById(R.id.title);
            groupHolder.imgView = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        TreeBean.Data category = mGroupArrList.get(groupPosition);
        String string = category.name;
        groupHolder.groupTitleView.setText(string);

        String jiaobiaoUrl = category.triangle;
        if (!TextUtils.isEmpty(jiaobiaoUrl) && !jiaobiaoUrl.equalsIgnoreCase("null")) {
            groupHolder.imgView.setVisibility(View.VISIBLE);
            String jinbaoUrl = "http://mall-dev.dev188.cn/fs/" + jiaobiaoUrl + "_40.png";//_126
            Glide.with(mContext).load(jiaobiaoUrl).into(groupHolder.imgView);
        } else {
            groupHolder.imgView.setVisibility(View.GONE);
        }
		/*int boldStyle = category.getBold();
		TextPaint tp = groupHolder.groupTitleView.getPaint();
		if (boldStyle == 1) {
			tp.setFakeBoldText(true);
		} else {
			tp.setFakeBoldText(false);
		}*/

        if (this.parentPosition == groupPosition) {
            groupHolder.groupTitleView.setTextColor(mContext.getResources().getColor(R.color.black));
            groupHolder.groupTitleView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            groupHolder.groupTitleView.setTextSize(15);
            groupHolder.selectLine.setVisibility(View.VISIBLE);
        } else {
            groupHolder.groupTitleView.setTextColor(mContext.getResources().getColor(R.color.exable_item_normal_black));
            groupHolder.groupTitleView.setBackgroundColor(mContext.getResources().getColor(R.color.eb_gray));
            groupHolder.groupTitleView.setTextSize(13);
            groupHolder.selectLine.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.expendablelist_child_item, null);
            childHolder.childTitleView = (TextView) convertView.findViewById(R.id.child_title);
            childHolder.lineView = (View) convertView.findViewById(R.id.line);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        String string = mGroupArrList.get(groupPosition).childs.get(childPosition).name;
        childHolder.childTitleView.setText(string);

        // 重点代码
        if (selected.get(childPosition) && this.parentPosition == groupPosition) {
            childHolder.childTitleView.setTextColor(mContext.getResources()
                    .getColor(R.color.red));
            if (isSelect) {
                //切换动画效果
                childHolder.childTitleView.startAnimation(mScaleBigAnim);
            }
            isSelect = false;
        } else {
            childHolder.childTitleView.setTextColor(mContext.getResources().getColor(
                    R.color.exable_chile_item_normal_black));
        }
        return convertView;
    }

    class GroupHolder {
        View selectLine;
        TextView groupTitleView;
        ImageView imgView;
    }

    class ChildHolder {
        TextView childTitleView;
        View lineView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
